/*------------------------------------------------------*/
/*                                                      */
/* Copyright (C)	sky-mobi.com                        */
/* All rights reserved                                  */
/*                                                      */
/* Author   tristan.yang                                */
/*                                                      */
/* History  2011-5-10 created                           */
/*                                                      */
/*------------------------------------------------------*/

#ifndef __LIVE_SVR_MGR_H__
#define __LIVE_SVR_MGR_H__

#include "database.h"
#include "connprovider.h"
#include "mepdu.h"

class CLiveSvrMgrSink
{
public:
	virtual void OnLiveMediaUpdate(const s_LiveUpdate& updateInfo) = 0;
	virtual void OnLiveMediaData(const strKeyId& keyId, CRtMessageBlock& msg, DWORD dwchannel) = 0;

	virtual void OnMgStreamUpdate(const strKeyId& keyId, const vecStreamInfo& refVec, DWORD uptype) = 0;
	virtual ~CLiveSvrMgrSink(){};
};

class CLiveConnectSink
{
public:
	virtual void OnDisconnect(LPVOID pConn, const TransportAddress& liveAddr) = 0;
	virtual ~CLiveConnectSink(){};
};

// CLiveConnect has func the same with DbThread in rscentmgr.
class CLiveConnect
: public CRtReferenceControlSingleThread
, public IConnectionSink
{
public: 
	CLiveConnect(CLiveSvrMgrSink* pSink, CLiveConnectSink* pConnSink, IConnection* pConn);
	virtual ~CLiveConnect();
	
	RtResult Initialize();
	RtResult GetOption(DWORD aOption, LPVOID lpData);
	
public:
	// from IConnectionSink
	virtual void OnReceiveData(CRtMessageBlock& aData, IConnection* pConn);
	virtual void OnDisconnect(RtResult aReason, IConnection* pConn);
	virtual void OnSend(IConnection* pConn);

private:
	RtResult HandleLiveChannelUpdate(const CMePduLiveChannelUpdate& pdu);
	RtResult HandleLiveAvCombineData(const CMePduLiveAvCombineData& pdu);

	// add for mg support by tristan at 2011-12-09
	RtResult HandleMgProgramUpdate(const CMePduMgProgramUpdate& pdu);
	RtResult HandleMgProgramStreamUpdate(const CMePduMgProgramStreamUpdate& pdu);
	RtResult HandleMgVideoData(CRtMessageBlock& msg);
	RtResult HandleMgAudioData(CRtMessageBlock& msg);
	// end add

private:
	CLiveSvrMgrSink*	m_pMgrSink;
	CLiveConnectSink*	m_pConnectSink;
	CRtAutoPtr<IConnection>	m_pConnect;
};

class CLiveSvrMgr
: public CRtReferenceControlSingleThread
, public IConnProviderSink
, public CLiveConnectSink
{
public: 
	CLiveSvrMgr();
	virtual ~CLiveSvrMgr();
	
	RtResult Initialize(/*const VecAddrType& vLiveAddrs,*/
						CLiveSvrMgrSink* pSink);
	
	RtResult LiveSvrUpdate(const VecAddrType& vLiveAddrs,
						DWORD upType);
	
public:
	// from IConnProviderSink
	virtual void OnConnectionReady(
							 RtResult aResult,
							 IConnection* pConn, // pAddr!=NULL if getten by connector; pAddr==NULL if getten by acceptor
							 const CRtInetAddr* pAddr);

	// from CLiveConnectSink
	virtual void OnDisconnect(LPVOID pConn, const TransportAddress& liveAddr);

private:
	RtResult AddLiveSvr(const TransportAddress& liveAddr);
	RtResult DelLiveSvr(const TransportAddress& liveAddr);

	// add for check if live svr connected
	BOOL FindIfConnected(const TransportAddress& liveAddr);

private:
	CLiveSvrMgrSink*	m_pSvrMgrSink;

	IConnProvider*		m_pConnProvider;

	typedef std::vector<CLiveConnect*>	VecLiveConnType;
	VecLiveConnType		m_vecLiveConnecters;

	VecAddrType			m_vecLiveAddrs;
};

#endif//__LIVE_SVR_MGR_H__
